/**
 * 
 */
package com.gr.staffpm.pages.dashboard.gadgets;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

import com.gr.staffpm.datatypes.Activity;
import com.gr.staffpm.notes.activity.service.IActivityDaoService;
import com.gr.staffpm.pages.notes.GroupNotesPanel;
import com.gr.staffpm.security.service.UserService;


/**
 * @author Graham Rhodes 5 Apr 2011 15:22:23
 */
public class ActivityGadget extends Panel {

    private static final long serialVersionUID = 1L;
    @SpringBean(name = "activityService")
    private IActivityDaoService activityDaoService;
    @SpringBean(name = "userService")
    private UserService userService;

    public ActivityGadget(String id) {
        super(id);
        boolean empty = true;

        Label noActivityLabel = new Label("noActivityLabel", "No activity in the last month on any of your tasks :(");

        RepeatingView groupNotes = new RepeatingView("groupNotes");

        DateTime dateOfActivity = new DateTime();
        List<Activity> activities = activityDaoService.getActivityForUserAndDate(userService.getCurrentUser(), dateOfActivity.toDate());
        for (int count = 0, i = 0; i < 31; i++) {
            if (activities != null && activities.size() > 0) {
                empty = false;
                groupNotes.addOrReplace(new GroupNotesPanel(String.valueOf(i), dateOfActivity, new ArrayList<Activity>(activities)));
                count++;
            }
            dateOfActivity = dateOfActivity.minusDays(1);
            activities = activityDaoService.getActivityForUserAndDate(userService.getCurrentUser(), dateOfActivity.toDate());
            if (count == 6)
                break;
        }
        noActivityLabel.setVisible(empty);
        add(noActivityLabel);
        add(groupNotes);

    }


}
